﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using IterationLife.ContentResources.Enums;
using IterationLife.Extensions.Dtos;
using Util;
using Util.Domains;
using Util.Domains.Auditing;

namespace IterationLife.ContentResources.Models
{
    /// <summary>
    /// 内容资源
    /// </summary>
    [Description("内容资源")]
    public partial class ContentResource : AggregateRoot<ContentResource>, IDelete, IAudited
    {
        /// <summary>
        /// 初始化内容资源
        /// </summary>
        public ContentResource() : this(Guid.Empty)
        {
        }

        /// <summary>
        /// 初始化内容资源
        /// </summary>
        /// <param name="id">内容资源标识</param>
        public ContentResource(Guid id) : base(id)
        {
            ContentResourceComments = new List<ContentResourceComment>();
            ContentResourcePictures = new List<ContentResourcePicture>();
            SingleBanner = new Resources();
            MultiBanners = new List<Resources>();
            SystemBanners = new List<Resources>();
            Video = new Resources();
        }

        /// <summary>
        /// 分类编号
        /// </summary>
        public Guid? CategoriyId { get; set; }
        /// <summary>
        /// 类型
        /// </summary>
        [Required(ErrorMessage = "类型不能为空")]
        public ContentResourceType? Type { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        [Required(ErrorMessage = "标题不能为空")]
        [StringLength(100, ErrorMessage = "标题输入过长，不能超过100位")]
        public string Title { get; set; }
        /// <summary>
        /// 单封面图
        /// </summary>
        [NotMapped]
        public Resources SingleBanner { get; set; }
        /// <summary>
        /// 单封面图Json字符串
        /// </summary>
        [StringLength(300, ErrorMessage = "单封面图Json字符串输入过长，不能超过300位")]
        public string SingleBannerJsonStr
        {
            get => Util.Json.ToJson(SingleBanner);
            set => SingleBanner = Util.Json.ToObject<Resources>(value);
        }
        /// <summary>
        /// 多封面图
        /// </summary>
        [NotMapped]
        public List<Resources> MultiBanners { get; set; }
        /// <summary>
        /// 多封面图Json字符串
        /// </summary>
        [StringLength(800, ErrorMessage = "多封面图Json字符串输入过长，不能超过800位")]
        public string MultiBannersJsonStr
        {
            get => Util.Json.ToJson(MultiBanners);
            set => MultiBanners = Util.Json.ToObject<List<Resources>>(value);
        }
        /// <summary>
        /// 系统封面集合
        /// </summary>
        [NotMapped]
        public List<Resources> SystemBanners { get; set; }
        /// <summary>
        /// 系统封面集合Json字符串
        /// </summary>
        [StringLength(800, ErrorMessage = "系统封面集合Json字符串输入过长，不能超过800位")]
        public string SystemBannersJsonStr
        {
            get => Util.Json.ToJson(SystemBanners);
            set => SystemBanners = Util.Json.ToObject<List<Resources>>(value);
        }
        /// <summary>
        /// 封面类型
        /// </summary>
        [Required(ErrorMessage = "封面类型不能为空")]
        public ContentResourceBannerType? BannerType { get; set; }
        /// <summary>
        /// 富文本
        /// </summary>
        public string RichText { get; set; }
        /// <summary>
        /// 视频
        /// </summary>
        [NotMapped]
        public Resources Video { get; set; }
        /// <summary>
        /// 视频Json字符串
        /// </summary>
        [StringLength(500, ErrorMessage = "视频Json字符串输入过长，不能超过500位")]
        public string VideoJsonStr
        {
            get => Util.Json.ToJson(Video);
            set => Video = Util.Json.ToObject<Resources>(value);
        }
        /// <summary>
        /// 简介
        /// </summary>
        [StringLength(1000, ErrorMessage = "简介输入过长，不能超过1000位")]
        public string BriefIntroduction { get; set; }
        /// <summary>
        /// 标签字符串
        /// </summary>
        [StringLength(800, ErrorMessage = "标签字符串输入过长，不能超过800位")]
        public string Tags { get; set; }
        /// <summary>
        /// 是否定时发表
        /// </summary>
        [Required(ErrorMessage = "是否定时发表不能为空")]
        public bool? IsRegularPublication { get; set; }
        /// <summary>
        /// 发表时间
        /// </summary>
        public DateTime? PublicationTime { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        [Required(ErrorMessage = "状态不能为空")]
        public ContentResourceState? State { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreationTime { get; set; }
        /// <summary>
        /// 创建人
        /// </summary>
        public Guid? CreatorId { get; set; }
        /// <summary>
        /// 最后修改时间
        /// </summary>
        public DateTime? LastModificationTime { get; set; }
        /// <summary>
        /// 最后修改人
        /// </summary>
        public Guid? LastModifierId { get; set; }
        /// <summary>
        /// 是否删除
        /// </summary>
        [Required(ErrorMessage = "是否删除不能为空")]
        public bool IsDeleted { get; set; }

        /// <summary>
        /// 内容资源评论列表
        /// </summary>
        public virtual ICollection<ContentResourceComment> ContentResourceComments { get; set; }

        /// <summary>
        /// 内容资源图集列表
        /// </summary>
        public virtual ICollection<ContentResourcePicture> ContentResourcePictures { get; set; }

        /// <summary>
        /// 内容资源分类
        /// </summary>
        public virtual ContentResourceCategory CategoriyContentResourceCategory { get; set; }

        /// <summary>
        /// 添加描述
        /// </summary>
        protected override void AddDescriptions()
        {
            AddDescription("内容资源编号", Id);
            AddDescription("分类编号", CategoriyId);
            AddDescription("类型", Type);
            AddDescription("标题", Title);
            AddDescription("单封面图", SingleBanner);
            AddDescription("多封面图", MultiBanners);
            AddDescription("系统封面集合", SystemBanners);
            AddDescription("封面类型", BannerType);
            AddDescription("富文本", RichText);
            AddDescription("视频", Video);
            AddDescription("简介", BriefIntroduction);
            AddDescription("标签", Tags);
            AddDescription("是否定时发表", IsRegularPublication.Description());
            AddDescription("发表时间", PublicationTime);
            AddDescription("状态", State);
            AddDescription("创建时间", CreationTime);
            AddDescription("创建人", CreatorId);
            AddDescription("最后修改时间", LastModificationTime);
            AddDescription("最后修改人", LastModifierId);
        }

        /// <summary>
        /// 添加变更列表
        /// </summary>
        protected override void AddChanges(ContentResource newEntity)
        {
            AddChange("Id", "内容资源编号", Id, newEntity.Id);
            AddChange("CategoriyId", "分类编号", CategoriyId, newEntity.CategoriyId);
            AddChange("Type", "类型", Type, newEntity.Type);
            AddChange("Title", "标题", Title, newEntity.Title);
            AddChange("SingleBanner", "单封面图", SingleBanner, newEntity.SingleBanner);
            AddChange("MultiBanners", "多封面图", MultiBanners, newEntity.MultiBanners);
            AddChange("SystemBanners", "系统封面集合", SystemBanners, newEntity.SystemBanners);
            AddChange("BannerType", "封面类型", BannerType, newEntity.BannerType);
            AddChange("RichText", "富文本", RichText, newEntity.RichText);
            AddChange("Video", "视频", Video, newEntity.Video);
            AddChange("BriefIntroduction", "简介", BriefIntroduction, newEntity.BriefIntroduction);
            AddChange("Tags", "标签", Tags, newEntity.Tags);
            AddChange("IsRegularPublication", "是否定时发表", IsRegularPublication, newEntity.IsRegularPublication);
            AddChange("PublicationTime", "发表时间", PublicationTime, newEntity.PublicationTime);
            AddChange("State", "状态", State, newEntity.State);
            AddChange("CreationTime", "创建时间", CreationTime, newEntity.CreationTime);
            AddChange("CreatorId", "创建人", CreatorId, newEntity.CreatorId);
            AddChange("LastModificationTime", "最后修改时间", LastModificationTime, newEntity.LastModificationTime);
            AddChange("LastModifierId", "最后修改人", LastModifierId, newEntity.LastModifierId);
        }
    }
}